﻿using STF.Blog.Dtos;
using STF.Blog.IService;
using STF.Blog.Model;
using STF.Blog.IRepository;
using STF.Blog.Repository;

namespace STF.Blog.Service
{
    public class UsersService : IUsersService
    {
        private IUsersManager _ium;
        public UsersService(IUsersManager ium)
        {
            _ium = ium;
        }

        public async Task<int> AddUsersAsync(string email, string pwd, string nickname, int state)
        {
            return await _ium.AddAsync(new Users
            {
                Email = email,
                Pwd = pwd,
                NickName = nickname,
                StateCode = state
            });
        }

        public async Task<int> DeleteUsersAsync(int id)
        {
            var data = await _ium.GetById(id);
            if (data == null)
                return -1;
            if (data.DeleteId != 1)
                return -2;
            return await _ium.DeleteAsync(data);
        }

        public async Task<List<UsersDto>> GetAllUsersAsync(int deleteId = 1)
        {
            var data = await _ium.GetChooseAsync(u => u.DeleteId == deleteId);
            return data == null ? new List<UsersDto>()
                : data.Select(u => new UsersDto
                {
                    Id = u.Id,
                    Email = u.Email,
                    Pwd = u.Pwd,
                    NickName = u.NickName,
                    StateCode = u.StateCode,
                    UpdateTime = u.UpdateTime
                }).ToList();
        }

        public async Task<int> UpdateUsersAsync(int id, string email, string pwd, string nickname, int state)
        {
            var data = await _ium.GetById(id);
            if (data == null)
                return -1;
            data.Email = email;
            data.Pwd = pwd;
            data.NickName = nickname;
            data.StateCode = state;
            data.UpdateTime = DateTime.Now;
            return await _ium.UpdateAsync(data);
        }

        public async Task<List<UsersDto>> GetByNameAsync(string nickname, int deleteId = 1)
        {
            var data = await _ium.GetChooseAsync(u => u.DeleteId == deleteId && u.NickName.Contains(nickname));
            return data == null ? new List<UsersDto>() : data.Select(u => new UsersDto
            {
                Id = u.Id,
                Email = u.Email,
                Pwd = u.Pwd,
                NickName = u.NickName,
                StateCode = u.StateCode,
                UpdateTime = u.UpdateTime
            }).ToList();
        }

        public async Task<UsersDto> GetByIdAsync(int id)
        {
            var data = await _ium.GetById(id);
            return data == null ? new UsersDto() : new UsersDto
            {
                Id = data.Id,
                Email = data.Email,
                Pwd = data.Pwd,
                NickName = data.NickName,
                StateCode = data.StateCode,
                UpdateTime = data.UpdateTime
            };
        }

        public async Task<int> PutTrashAsync(int id)
        {
            var data = await _ium.GetById(id);
            if (data == null || data.DeleteId == 0)
                return -1;
            data.DeleteId = 0;
            return await _ium.UpdateAsync(data);
        }

        public async Task<int> RestoreAsync(int id)
        {
            var data = await _ium.GetById(id);
            if (data == null || data.DeleteId == 1)
                return -1;
            data.DeleteId = 1;
            return await _ium.UpdateAsync(data);
        }

        public async Task<int> LoginAsync(string account, string pwd)
        {
            var data = await _ium.GetChooseAsync(us => us.Email.Equals(account) && us.Pwd.Equals(pwd));
            return data.Count == 0 ? -1 : data[0].Id;
        }

        public async Task<int> GetUserByNameAsync(string nickname, int deleteId = 1)
        {
            var data = await _ium.GetChooseAsync(us => us.NickName.Equals(nickname) && us.DeleteId == deleteId);
            return data != null ? data[0].Id : -1;
        }
    }
}